<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="5" :md="6" :sm="24">
            <a-form-item :label="$t('卡号')">
              <a-input :placeholder="$t('请输入卡号')" v-model="queryParam.cardSn"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('业务类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择业务类型')"
                  v-model="queryParam.typeId"
                  dictCode="bis_member_coin_record.type_id"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('机台名称')">
                <j-search-select-tag
                  :placeholder="$t('请选择机台名称')"
                  v-model="queryParam.gameId"
                  dict="bis_game,name,id"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="5" :md="6" :sm="24">
              <a-form-item :label="$t('机位号')">
                <a-input :placeholder="$t('请输入机位号')" v-model="queryParam.machineNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('设备名称(编码)')">
                <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                    <span>{{ item.name }}</span>
                    <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-select v-model="queryParam.createBy">
                  <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                    item.realname
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'BisMemberCoinRecordController:export'" type="primary" icon="download" @click="handleExportXls($t('数字币记录'))">{{ $t('导出') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <template slot="footer">
          <div style="font-weight: bold">
            <span>{{ $t('总币值') }}：{{ totalInfo.value.toFixed(2) }}</span>
          </div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import moment from 'moment'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'BisMemberCoinRecordList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      moment,
      description: this.$t('数字币记录管理页面'),
      // 表头
      columns: [
        {
          title: this.$t('时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'cardSn',
        },
        {
          title: this.$t('业务类型'),
          align: 'center',
          dataIndex: 'typeId_dictText',
        },
        {
          title: this.$t('机台名称'),
          align: 'center',
          dataIndex: 'gameId_dictText',
        },
        {
          title: this.$t('机位号'),
          align: 'center',
          dataIndex: 'machineNo',
        },
        {
          title: this.$t('交易前币'),
          align: 'right',
          dataIndex: 'valueBefore',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('交易币'),
          align: 'right',
          dataIndex: 'value',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('交易后币'),
          align: 'right',
          dataIndex: 'valueAfter',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceId_dictText',
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'createBy',
        },
      ],
      url: {
        list: '/coin/bisMemberCoinRecord/list',
        delete: '/coin/bisMemberCoinRecord/delete',
        deleteBatch: '/coin/bisMemberCoinRecord/deleteBatch',
        exportXlsUrl: '/coin/bisMemberCoinRecord/exportXls',
        importExcelUrl: 'coin/bisMemberCoinRecord/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      totalCallback: this.totalCallbacks,
      deviceList: [],
      userList: [],
      queryParam: {
        deviceId: '',
        createBy: '',
      },
    }
  },
  created() {
    this.getSuperFieldList()
    this.initDictConfig()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.handleSearch('')
      getAction('/sys/user/list').then((res) => {
        if (res.success) {
          this.userList = res.result.records
          this.userList.forEach((col) => {
            col.id = col.realname
          })
          this.userList.unshift({ id: '', realname: this.$t('全部') })
        }
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'datetime', value: 'createTime', text: '业务时间' })
      fieldList.push({ type: 'string', value: 'cardSn', text: '卡号', dictCode: '' })
      fieldList.push({ type: 'int', value: 'typeId', text: '业务类型', dictCode: 'bis_member_coin_record.type_id' })
      fieldList.push({
        type: 'sel_search',
        value: 'gameId',
        text: '机台名称',
        dictTable: 'bis_game',
        dictText: 'name',
        dictCode: 'id',
      })
      fieldList.push({ type: 'int', value: 'machineNo', text: '机位号', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'valueBefore', text: '交易前币', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'value', text: '交易币', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'valueAfter', text: '交易后币', dictCode: '' })
      fieldList.push({
        type: 'sel_search',
        value: 'deviceId',
        text: '设备编码',
        dictTable: 'bis_device_info',
        dictText: 'name',
        dictCode: 'id',
      })
      this.superFieldList = fieldList
    },
    totalCallbacks(totalInfo) {
      this.totalInfo = totalInfo
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    searchReset(){
      this.queryParam = {
        deviceId: '',
        createBy: '',
      }
      this.selectedRowIndex = []
      this.loadData(1)
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>